/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : app_freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os2.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "adc.h"
#include "tim.h"
#include "usart.h"
#include "mytcp.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
extern uint32_t points[1000];
extern int adc_start_flag;
extern uint8_t USART5_RxBuff[1024];
extern volatile int uart5_rcv_flag;
/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 128 * 4
};
/* Definitions for myTask02 */
osThreadId_t myTask02Handle;
const osThreadAttr_t myTask02_attributes = {
  .name = "myTask02",
  .priority = (osPriority_t) osPriorityLow,
  .stack_size = 128 * 4
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void StartTask02(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of myTask02 */
  myTask02Handle = osThreadNew(StartTask02, NULL, &myTask02_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}
/* USER CODE BEGIN Header_StartDefaultTask */
/**
* @brief Function implementing the defaultTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN defaultTask */
  /* Infinite loop */
	printf("StartDefaultTask[start]\r\n");
	//HAL_ADC_Start_DMA(&hadc4, points, 1000);
	
	
  for(;;)
  {
		printf("StartDefaultTask[running...],uart5_rcv_flag=[%d]\r\n", uart5_rcv_flag);
//		if(adc_start_flag == 1){
//			//处理数据
//			printf("开始处理数据\r\n");
//			unsigned int total=0;
//			for(int i=0;i<1000;i++){
//				total += points[i];
//			}
//			printf("平均值[%d]\r\n", (total/1000));
//			HAL_ADC_Start_DMA(&hadc4, points, 1000/2);
//			osDelay(5000);
//			//再次开启采集
//			adc_start_flag = 0;
//			memset(points, 0, sizeof(points));
//			HAL_ADC_Start_DMA(&hadc4, points, 1000);
//		}
    osDelay(1000);
  }
  /* USER CODE END defaultTask */
}

/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the myTask02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void *argument)
{
  /* USER CODE BEGIN myTask02 */
  /* Infinite loop */
	//esp8266初始化
	printf("StartTask02[start]\r\n");
  for(;;)
  {
		if(uart5_rcv_flag != 1)
			continue;
		int msg_type;
		char* p = (char*)USART5_RxBuff;
		memcpy(&msg_type, USART5_RxBuff, 4);
		printf("msg_type=[%d]\r\n", msg_type);
		if(msg_type == 1){
			//数据采集指令
			adc_start_flag = 0;
			memset(points, 0, sizeof(points));
			HAL_ADC_Start_DMA(&hadc4, points, 1000);
			//等待完成数据采集
			printf("等待完成ADC采样\r\n");
			while(adc_start_flag!=1){};
			//透传模式发送数据
			unsigned int total=0;
			uint32_t temp=2;
			my_tcp_send((char*)&temp, 4);
			temp=4000;
			my_tcp_send((char*)&temp, 4);
			osDelay(500);
			for(int i=0;i<1000/40;i++){
				my_tcp_send((char*)(points+i*40), 40*4);
				osDelay(500);
			}
			printf("完成了数据的透传\r\n");
		}
		//
		uart5_rcv_flag=0;
		memset(USART5_RxBuff, 0 , 1024);
		
		
//		if(esp_recv_flag != 1)
//			continue;
//		//处理串口数据
//		printf("[Task02]任务中处理回复消息\r\n");
//		//业务处理
//		//1\如果是采集波形的指令
//		//2\采集波形
//		//3\发送波形数据
//		//4\重新开启接收数据
//		esp_recv_flag = 0;
//		memset(recvData, 0 , 1024);
//		HAL_UART_Receive_DMA(&huart5, recvData, 1024);
		
  }
  /* USER CODE END myTask02 */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

